'use strict';

module.exports = app => {
    return class BbsService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 获取社区首页
         * @param params
         */
        async bbs() {
            let url = this.app.urls('bbs');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 查看更多分类
         * @param params
         */
        async bbsmore() {
            let url = this.app.urls('bbsmore');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /*
        查看更多问答
         */
        async getanswerlist(){
            let url = this.app.urls('getanswerlist');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url,{
                method:'get',
                dataType:'json'
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 根据分类编号查看社区
         * @param params
         */
        async bbscont(params) {
            let url = this.app.urls('bbscont');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id:params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /*
        *获取社区帖子详情
         */
        async postdetail(params){
            let url = this.app.urls('postdetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:params,
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;

        }

        /*
        获取社区个人收藏(社区)
         */
        async collection(){
            let url = this.app.urls('myinteract');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;
        }

        /*
        获取社区个人收藏(帖子和热评)
        */
        async collectionpost(params){
            let url = this.app.urls('mypost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    type:params,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;
        }

        /*
        社区搜索功能
         */

        async search(params){
            let url = this.app.urls('search');
            const result = await this.ctx.fetch(url,{
                method:'get',
                dataType:'json',
                data:{
                    searchName:params.searchName,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;

        }


        /*
        社区点赞
         */
        async clicklike(params){
            let url = this.app.urls('clicklike');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    type:params.type,
                    objectId:params.objectId,
                    domainId:params.domainId,
                    roleType:params.roleType,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;
        }
    }
};
